<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="90px"
    >
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入药材编码/药材名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="药材毒性" prop="toxicity">
        <el-select
          v-model="queryParams.toxicity"
          placeholder="请选择药材毒性"
          clearable
          @change="handleQuery"
        >
          <el-option label="无毒" :value="0"> </el-option>
          <el-option label="有毒" :value="1"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="药材类别" prop="categoryIds">
        <el-select
          v-model="queryParams.categoryIds"
          placeholder="请选择药材类别"
          clearable
          filterable
          multiple
          collapse-tags
          style="width: 100%"
          @change="handleQuery"
        >
          <el-option
            v-for="item in categoryOptions"
            :key="item.categoryId"
            :label="item.categoryName"
            :value="item.categoryId"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOpen()"
          >新建药材</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-view"
          plain
          size="mini"
          @click="goClashList"
          >十八反十九畏</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button plain icon="el-icon-edit" size="mini" @click="handBatchSet"
          >批量设置最大用量</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-menu"
          plain
          size="mini"
          @click="handClassManage"
          >药材类别管理</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList" ref="table">
      <el-table-column
        type="selection"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column label="药材编码" align="center" prop="code" />
      <el-table-column label="药材名称" align="center" prop="name" />
      <el-table-column label="药材图片" align="center" prop="materialsImg">
        <template slot-scope="scope">
          <image-preview
            v-if="scope.row.materialsImg"
            :src="scope.row.materialsImg"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="药材首字母" align="center" prop="firstLetter" />
      <el-table-column label="药材类别" align="center" prop="categoryNames" width="200" />
      <el-table-column label="药材性味" align="center" prop="materialsTrait" width="200"  />
      <el-table-column label="药材功能" align="center" prop="materialsEffect" width="200" />
      <el-table-column
        label="药材主治"
        align="center"
        prop="materialsIndicate"
        width="200"
      />
      <el-table-column label="药材简介" align="center" prop="note" width="200">
        <template slot-scope="{ row }">
          <el-tooltip effect="dark" :content="row.note" placement="top">
            <div class="ov-6">{{ row.note }}</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="药材毒性" align="center">
        <template slot-scope="{ row }">
          <el-tag v-if="row.toxicity === 0" size="small">无毒</el-tag>
          <el-tag v-else size="small" type="danger">有毒</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="药材经脉"
        align="center"
        prop="materialsMeridians"
        width="200"
      />
      <el-table-column label="建议用量" align="center" prop="dosageAdvice" width="200" />
      <el-table-column label="最大用量" align="center" prop="doseMax" />
      <el-table-column
        label="药材状态"
        align="center"
        prop="status"
        width="140"
      >
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="1"
            active-text="启用"
            :inactive-value="0"
            inactive-text="禁用"
            @change="changeStatus(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="150"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleOpen(scope.row.id)"
            >编辑</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <DrugEdit
      ref="DrugEdit"
      :categoryOptions="categoryOptions"
      @refresh="getList"
    />
    <BatchEdit ref="BatchEdit" @refresh="getList" />
  </div>
</template>

<script>
import { getDrugList, deleteDrug, updateDrug, getClassManageList } from "@/api/saas/tcm.js";
import DrugEdit from "./drugEdit";
import BatchEdit from "./batchEdit"
import { deepClone } from '@/utils';

export default {
  name: "Drug",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: "",
        toxicity: undefined,
        categoryIds: []
      },
      // 总条数
      total: 0,
      dataList: [],
      categoryOptions: []
    };
  },
  components: {
    DrugEdit,
    BatchEdit
  },
  created() {
    this.getList();
    this.getDrugClassList();
  },
  activated() {
    this.getList();
    this.getDrugClassList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      const params = deepClone(this.queryParams);
      params.categoryIds = params.categoryIds.join(",");
      getDrugList(params)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 获取类别列表
    getDrugClassList() {
      getClassManageList({ pageNum: 1, pageSize: 999 }).then(res => {
        this.categoryOptions = res.rows;
      })
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      Object.assign(this.$data.queryParams, this.$options.data().queryParams);
      this.getList();
    },
    handleOpen(id) {
      this.$refs.DrugEdit.openModal(id);
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      this.$modal
        .confirm("是否确认删除该药材吗？")
        .then(function () {
          return deleteDrug(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    // 修改状态
    changeStatus(row) {
      updateDrug(row).then(res => {
        this.$message.success('状态修改成功');
      }).catch((err) => {
        row.status = row.status ? 0 : 1; // 修改失败,复位
      })
    },
    // 批量设置最大用量
    handBatchSet() {
      const ids = this.$refs.table.selection.map(v => v.id);
      if (!ids.length) {
        this.$message.error('请勾选需要设置的药材');
        return
      }
      this.$refs.BatchEdit.openModal(ids.join(','))
    },
    // 跳转去十八反十九畏
    goClashList() {
      this.$router.push({ path: '/saas/clashList/index' });
    },
    // 跳转去药材类别管理
    handClassManage() {
      this.$router.push({ path: '/saas/classManage/index' });
    }
  },
};
</script>

<style lang="scss" scoped></style>
